package com.smb.app.android.stringstools;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import com.alibaba.excel.metadata.Sheet;

/**
 * Hello world!
 *
 */
public class App {
	private static final int MAPPING_PAGE = 1;
	private static final int MAPPING_SHEET_ID_COLUMN = 0;
	private static final int MAPPING_SHEET_AID_COLUMN = 1;

	private static final int STRINGS_PAGE = 2;

	private static final int STRINGS_SHEET_ID_COLUMN = 14;
	private static final int STRINGS_SHEET_ENG_COLUMN = 15;

	private static final int WEB_STRINGS_PAGE = 3;
	private static final int WEB_SHEET_ID_COLUMN = 0;
	private static final int WEB_SHEET_ENG_COLUMN = 1;

	private static final String NO_VALUE_TAG = "noValue";

	private static String excelPath;
	private static int columnNum;

	private static ArrayList<KeyValueBean> mappingList = new ArrayList<KeyValueBean>();
	private static ArrayList<KeyValueBean> sourceList = new ArrayList<KeyValueBean>();

	public static void main(String[] args) {

		doit();

	}

	static void doit() {
		Scanner cScanner = new Scanner(System.in);
		System.out.print("输入总表路径：");
		excelPath = cScanner.next();
		System.out.println("输入目标生成多语言文件类型 ，“1” for strings.xml ,“2” for strings.properties");
		System.out.println("输入：");
		String string = cScanner.next();

		switch (string) {
		case "1":
			System.out.println("你输入的是：" + string + " 生成  strings.xml");
			System.out.print("把待生成的目标生成语言列插入总表strings Sheet其中的一列，AS列为 44 。插入列数是：");
			columnNum = cScanner.nextInt();
			System.out.println("你输入的列数是：" + columnNum);

			System.out.print("空缺值是否用英语补上：“1” for Yes ,“2” for No:");
			int i = cScanner.nextInt();
			System.out.println("注意：生成的strings文件没有string-array,需手动补上!!!!!!");
			System.out.println("注意：生成的strings文件没有占位符不对,需手动改正!!!!!!");
			System.out.println("开始生成...");
			long time = System.currentTimeMillis();
			generateStringsXML(i == 1 ? true : false);
			long time1 = System.currentTimeMillis();
			System.out.printf("用时%dms", time1 - time);

			break;
		case "2":
			System.out.println("你输入的是：" + string + " 生成 strings.properties");
			System.out.print("把待生成的目标生成语言列插入总表web侧Strings Sheet其中的一列，AE列为 30。插入列数是：");
			columnNum = cScanner.nextInt();
			System.out.println("你输入的列数是：" + columnNum);
			System.out.print("空缺值是否用英语补上：“1” for Yes ,“2” for No:");
			int j = cScanner.nextInt();
			System.out.println("开始生成...");
			long time2 = System.currentTimeMillis();
			generateStringsProperties(j == 1 ? true : false);
			long time3 = System.currentTimeMillis();
			System.out.printf("用时%dms", time3 - time2);

			break;
		default:
			System.out.println("输入不合法，“1” for strings.xml ,“2” for strings.properties");
			doit();
			break;
		}

		cScanner.close();
	}

	static void generateStringsXML(boolean isNeedEngMakeup) {

		readSheet(MAPPING_PAGE, mappingList, MAPPING_SHEET_ID_COLUMN, MAPPING_SHEET_AID_COLUMN, 0, false);
		readSheet(STRINGS_PAGE, sourceList, STRINGS_SHEET_ID_COLUMN, columnNum, STRINGS_SHEET_ENG_COLUMN,
				isNeedEngMakeup);

		for (int i = 0; i < sourceList.size(); i++) {
			for (int j = 0; j < mappingList.size(); j++) {
				if (sourceList.get(i).equals(mappingList.get(j))) {
					sourceList.get(i).setKey(mappingList.get(j).getValue());
					break;
				}
			}
		}

		int noKeyNum = 0;
		int noValueNum = 0;
		ArrayList<KeyValueBean> noValueList = new ArrayList<KeyValueBean>();
		for (KeyValueBean keyValueBean : sourceList) {
			boolean hasNoValue = false;
			if (keyValueBean.getKey().equals(NO_VALUE_TAG)) {
				hasNoValue = true;
				noKeyNum++;
			}

			if (keyValueBean.getValue().equals(NO_VALUE_TAG)) {
				hasNoValue = true;
				noValueNum++;
			}
			if (hasNoValue) {
				noValueList.add(keyValueBean);
				hasNoValue = false;
			}

		}
		System.out.printf("无KEY：%d个，无value：%d个\n", noKeyNum, noValueNum);
		System.out.println("无值字段包括：");
		for (KeyValueBean keyValueBean : noValueList) {
			String formatString = "<string name=\"%1s\">%2s</string>";
			System.out.println(String.format(formatString, keyValueBean.getKey(), keyValueBean.getValue()));
		}

		for (int i = sourceList.size() - 1; i > 0; i--) {
			KeyValueBean keyValueBean = sourceList.get(i);
			if (keyValueBean.getKey().equals("\\") || keyValueBean.getKey().equals("/")
					|| keyValueBean.getKey().equals(NO_VALUE_TAG)) {
				sourceList.remove(keyValueBean);
			}
		}

		StringsFileGenerator.generateStringsXML(sourceList, getFileName(false));
	}

	static void generateStringsProperties(boolean isNeedEngMakeup) {
		readSheet(WEB_STRINGS_PAGE, sourceList, WEB_SHEET_ID_COLUMN, columnNum, WEB_SHEET_ENG_COLUMN, isNeedEngMakeup);
		StringsFileGenerator.generatePropertiesStrings(sourceList, getFileName(true));

	}

	static void readSheet(int sheetPage, ArrayList<KeyValueBean> list, int idColumn, int valueColumn,
			int engMakeupColumn, boolean isNeedEngMakeup) {
		Sheet rs = new Sheet(sheetPage);
		List<ArrayList> objects = ExcelUtil.readMoreThan1000RowBySheet(excelPath, rs);
		for (ArrayList<String> arr : objects) {

			String keyString;
			String valueString;
			if (arr.get(idColumn) == null || arr.get(idColumn).equals("ID"))// 跳过第一行表头 和空行
				continue;

			if (arr.get(idColumn).equals("")) {
				keyString = NO_VALUE_TAG;
			} else {
				keyString = arr.get(idColumn);
			}
			if (arr.size() <= valueColumn || arr.get(valueColumn) == null || arr.get(valueColumn).equals("")) {
				if (isNeedEngMakeup) {
					valueString = arr.get(engMakeupColumn);
				} else {
					valueString = NO_VALUE_TAG;
				}

			} else {
				valueString = arr.get(valueColumn);
			}

			list.add(new KeyValueBean(keyString, valueString));
		}
	}

	static String getFileName(boolean isWebStrings) {

		String fileName = excelPath.substring(excelPath.lastIndexOf("\\") + 1);

		if (isWebStrings) {
			return excelPath.replace(fileName, "strings.properties");
		} else {
			return excelPath.replace(fileName, "strings.xml");
		}
	}
}
